//
//  UIViewController+HUD.m
//  YBInsurance
//
//  Created by S2 on 12/25/15.
//  Copyright © 2015 YB. All rights reserved.
//

#import "UIViewController+HUD.h"

@implementation UIViewController (HUD)

- (void)showHUDWith:(NSString *)text {
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        [self hideHUDNoDelay];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        hud.label.numberOfLines = 0;
        hud.label.font = [UIFont systemFontOfSize:16];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:3];
    });
}

- (void)viewWillAppear:(BOOL)animated {
    
}

- (void)showLoadingHUD:(NSString *)text {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self hideHUDNoDelay];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.label.text = text;
        hud.label.numberOfLines = 0;
        hud.label.font = [UIFont systemFontOfSize:16];
        hud.removeFromSuperViewOnHide = YES;
    });
}

- (void)delay:(CGFloat)duration action:(CallBack)callBack {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        callBack();
    });
}

- (void)repeat:(CGFloat)duration times:(CGFloat)times action:(CallBack)callBack {
    
    NSTimeInterval period = duration; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        
        callBack();
    });
    dispatch_resume(_timer);
}

/**
 *  @brief  隐藏页面上的HUD
 */

- (void)hideHUD {
    
    [self delay:1 action:^{
        
        //取消菊花
        
//        if ([MBProgressHUD allHUDsForView:[UIApplication sharedApplication].keyWindow].count > 0) {
//
//            [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
//        }
        
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
    }];
}

#pragma mark - Private

- (void)hideHUDNoDelay {
    
        //取消菊花
//    if ([MBProgressHUD allHUDsForView:[UIApplication sharedApplication].keyWindow].count > 0) {
//
//        [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
//    }
    [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
}

- (void)showSystemAlertViewWithMessage:(NSString *)message withSeconds:(CGFloat)seconds {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertVC animated:YES completion:nil];
    [NSTimer scheduledTimerWithTimeInterval:seconds repeats:NO block:^(NSTimer * _Nonnull timer) {
        [alertVC dismissViewControllerAnimated:YES completion:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
        
    }];
}

@end
